package com.example.campusrear.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.campusrear.Service.MeetingNoticeService;
import com.example.campusrear.Utils.R;
import com.example.campusrear.entity.MeetingNotice;
import com.example.campusrear.entity.MeetingNoticeDto;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Meeting")
public class MeetingNoticeController {
    @Resource
    private MeetingNoticeService meetingNoticeService;

    @PostMapping("/save")
    public R<String> save(@RequestBody MeetingNotice meetingNotice){
        meetingNoticeService.save(meetingNotice);
        return R.success("发布成功");
    }
    @GetMapping("/queryMeeting")
    public R<List<MeetingNoticeDto>> queryMeeting(Long cId){
        LambdaQueryWrapper<MeetingNotice> queryWrapper = new LambdaQueryWrapper<>();
        if (cId!=null){
            queryWrapper.eq(MeetingNotice::getClassId,cId);
        }
        List<MeetingNotice> list = meetingNoticeService.list(queryWrapper);
        List<MeetingNoticeDto> noticeDtoList = new ArrayList<>();
        for (MeetingNotice notice : list) {
            MeetingNoticeDto meetingNoticeDto = new MeetingNoticeDto();
            BeanUtils.copyProperties(notice,meetingNoticeDto);
            Date noticeTime = meetingNoticeDto.getNoticeTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String formattedDate = sdf.format(noticeTime);
            meetingNoticeDto.setStrTime(formattedDate);
            noticeDtoList.add(meetingNoticeDto);
        }
        return R.success(noticeDtoList);
    }
}
